import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

import '../../../../core/app_export.dart';


final ImagePicker _imagePicker = ImagePicker();



Future<File?> _getImageFromGallery()async {
  var pickedImage =
  await _imagePicker.pickImage(source: ImageSource.gallery);

  if (pickedImage != null) {
    return File(pickedImage.path);
  }
}
Future<File?> _getImageFromCamera()async {
  var pickedImage =
  await _imagePicker.pickImage(source: ImageSource.camera);
  if (pickedImage != null) {
    return File(pickedImage.path);
  }
}


class MyCircleAvatar extends StatelessWidget {
  final double? width;
  final double? activeIconWidth;
  final File? imageFile;
  final String? imageURL;
  final String? imagePath;
  final Color? borderColor;
  final Color? backgroundColor;
  final double? borderSize;
  final EdgeInsets? padding;
  final bool? editMode;
  final bool? active;
  final bool? offline;
  final Function(File?)? onSelectedImage;


  const MyCircleAvatar({
    Key? key,
    this.width,
    this.activeIconWidth = 16,
    this.imageFile,
    this.imageURL,
    this.imagePath,
    this.borderColor,
    this.backgroundColor,
    this.borderSize,
    this.padding,
    this.onSelectedImage,
    this.editMode = false,
    this.active,
    this.offline,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding:padding?? const EdgeInsets.all(0),
      child: Container(
        width: (width??20)*2,
        height: (width??20)*2,
        decoration: BoxDecoration(
          border: Border.all(color: borderColor?? Colors.orangeAccent,width: borderSize??2),
          shape: BoxShape.circle,
        ),
        child: Stack(
          children: [
            Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if(imageFile != null)
                    FutureBuilder(
                      future: imageFile?.readAsBytes(),
                      builder: (context , snapshot){
                        return CircleAvatar(
                          radius: (width??20)  - (borderSize??2),
                          backgroundColor: backgroundColor?? theme.scaffoldBackgroundColor,
                          backgroundImage: Image.memory(snapshot.data!,).image,
                        );
                      },
                    )
                  else if(imagePath!= null)
                    CircleAvatar(
                      radius: (width??20)  - (borderSize??2),
                      backgroundColor: backgroundColor?? theme.scaffoldBackgroundColor,
                      backgroundImage: Image.asset(imagePath!).image,
                    )
                  else if(imageURL!= null)
                    CircleAvatar(
                      radius: (width??20)  - (borderSize??2),
                      backgroundColor: backgroundColor?? theme.scaffoldBackgroundColor,
                      backgroundImage: CachedNetworkImageProvider(imageURL!),
                    )
                  else
                    Expanded(
                      child: Container(
                        clipBehavior: Clip.antiAlias,
                        decoration: BoxDecoration(
                          color: backgroundColor??theme.scaffoldBackgroundColor,
                          shape: BoxShape.circle,
                          border: Border.all(color: borderColor??Colors.grey,)
                        ),
                        padding:const EdgeInsets.all(8),
                        child: Image.asset(ImageConstant.icUserName, color: Colors.orange),
                      ),
                    ),

                ],
              ),
            ),
            if(editMode==true)
              Align(
                alignment: Alignment.bottomRight,
                child:InkWell(
                  onTap: ()async{
                    onSelectedImage?.call((await _getImageFromGallery()));
                  },
                  child: Container(
                      padding:const EdgeInsets.all(8),
                      decoration:BoxDecoration(
                        color: Colors.grey.withOpacity(.5),
                        shape: BoxShape.circle,
                      ),
                      child: Image.asset(ImageConstant.icEdit, color: Colors.green,width: 22,)),
                ),
              ),
            if(active==true)
              Align(
                alignment: Alignment.topRight,
                child:InkWell(
                  onTap: ()async{
                    //onSelectedImage?.call((await _getImageFromGallery()));
                  },
                  child: Container(
                      decoration:BoxDecoration(
                        color: Colors.grey.withOpacity(.5),
                        shape: BoxShape.circle,
                      ),
                      child: Image.asset(ImageConstant.icActive,width: activeIconWidth,)),
                ),
              ),
            if(active==false)
              Align(
                alignment: Alignment.topRight,
                child:InkWell(
                  onTap: ()async{
                    //onSelectedImage?.call((await _getImageFromGallery()));
                  },
                  child: Container(
                      decoration:BoxDecoration(
                        color: Colors.grey.withOpacity(.5),
                        shape: BoxShape.circle,
                      ),
                      child: Image.asset(ImageConstant.icWrong,width: activeIconWidth,)),
                ),
              ),

            if(offline==true)
              Container(
                  alignment: Alignment.topRight,
                  decoration:BoxDecoration(
                    color: Colors.grey.withOpacity(.5),
                    shape: BoxShape.circle,
                  ),
                  child: Image.asset(ImageConstant.icOffline,width: activeIconWidth,)),
          ],
        ),
      ),
    );
  }
}
